-------------------------------------------------------------------------------------
-- -*- coding: utf-8 -*-
--
-- @author:   zxf
-- @email:    07291@qq.com
-- @date:     2024/5/31
-- @module:   role
-- @describe: 角色
-- @version:  v1.0
--

-------------------------------------------------------------------------------------

---@class role
local role = {}
local this = role

local import = import
local game_unit = game_unit
local main_ctx = main_ctx
local actor_ctx = actor_ctx
local actor_unit = actor_unit
local local_player = local_player

---@type keycode_res
local keycode_res = import("game/resources/keycode_res")
---@type redis
local redis = import("game/func/redis")
---@type api
local api = import("api/api")
---@type configer
local configer = import("base/configer")

-- 按键点击
function this.key_control(key_str, action)
    if key_str == 'KEY_RightMouseButton' then
        game_unit.key_call_ex(key_str, action)
    elseif key_str == 'KEY_LeftMouseButton' then
        game_unit.key_call_ex(key_str, action)
    else
        local keycode = keycode_res.KEYCODE_INFO[key_str]
        if type(key_str) == 'number' then
            keycode = key_str
        end
        if keycode then
            if action then
                return main_ctx:post_key(keycode, action)
            else
                return main_ctx:do_skey(keycode)
            end
        end
    end
    return false
end

-- 获取角色数据通过类型 0 当前角色 1玩家 2 npc 3 怪物
function this.get_role_data(setType, name)
    local list = actor_unit.list(setType)
    local ret = nil
    for i = 1, #list do
        local obj = list[i]
        if actor_ctx:init(obj) then
            if not ret then
                ret = {}
            end
            if name and string.find(actor_ctx:name(), name) or not name then
                table.insert(ret, {
                    obj = obj,
                    --name_id = actor_ctx:name_id(),
                    class_name = actor_ctx:class_name(),
                    id = actor_ctx:id(),
                    cx = actor_ctx:cx(),
                    cy = actor_ctx:cy(),
                    cz = actor_ctx:cz(),
                    job = actor_ctx:job(),
                    --is_dead = actor_ctx:is_dead(),
                    --is_combat = actor_ctx:is_combat(),
                    name = actor_ctx:name()
                })
            end
        end
    end
    return ret
end

-- 是否转职
function this.check_class_name(matchVal)
    local roleData = role.get_role_data(0)
    local myData = roleData and roleData[1] or nil
    local className = "1"
    if myData then
        className = myData.class_name
    end
    if className and string.match(className, matchVal) then
        return true
    end
    return false
end

function this.get_role_idx(owner)
    local myName = owner and owner.name or local_player:name()
    local list = api.get_role_info()
    local idx = 0
    for k, v in ipairs(list) do
        if v.name == myName then
            idx = k
            break
        end
    end
    return idx
end

function role.update_role_dungeon_Data_to_ini(owner, Data, value, nType)
    local need_region = main_ctx:c_region_name()
    local need_server = main_ctx:c_server_name()
    local idx = this.get_role_idx(owner)
    local section = need_region .. "_" .. need_server .. "_" .. idx
    local setData = Data .. "_" .. nType
    local name = owner and owner.name or local_player:name()
    configer.set_user_profile(section, "name", name)
    redis.set_user_profile_today_four(section, setData, value)
    return true
end

function role.get_role_dungeon_Data_by_ini(Data, nType, owner)
    local need_region = main_ctx:c_region_name()
    local need_server = main_ctx:c_server_name()
    local idx = this.get_role_idx(owner)
    local section = need_region .. "_" .. need_server .. "_" .. idx
    if nType == "日" then
        return redis.get_user_profile_today_four(section, Data .. "_" .. nType)
    elseif nType == "周" then
        return redis.get_user_profile_to_monday(section, Data .. "_" .. nType)
    end
    return nil
end

function role.is_set_role(isSet, owner, mainTarget, minLevel, maxLevel, nType)
    if isSet then
        for i = 0, 3 do
            table.insert(owner.roleInfo[i], {
                mainName = mainTarget,
                minLevel = minLevel,
                maxLevel = maxLevel,
                nType = nType,
                isSet = isSet
            })
        end
    else
        table.insert(owner.roleInfo[0], {
            mainName = mainTarget,
            minLevel = minLevel,
            maxLevel = maxLevel,
            nType = nType,
            isSet = false,
        })
    end
end

return this
